function loadInitFile(a_tag_id,removeFile,downloadFile,magnifyFile,fileId,customerId,fileType) {
    $.post(sysInfo.basePath + "/annex/find_files.do", {fileId: fileId,customerId:customerId,type:fileType}, function (result) {
        if (result.code == 0) {
            var data = result.data;
            if(data.length > 0){
                $.each(data, function (index, file) {
                    appendFile(file,a_tag_id,removeFile,downloadFile,magnifyFile);
                })
            }
        } else {
            dggui.dialog.errors(result.msg);
        }
    });
}

function appendFile(file,a_tag_id,removeFile,downloadFile,magnifyFile){
    var ext = getFileExt(file.filepath);
    var htm = new Array();
    htm.push("<li><div class=\"upload-img\">");
    if (isImgFile(file.filename)) {
        htm.push("<img src=\""+file.filepath+"\">");
    }else{
        htm.push("<i class=\"iconfont "+getFileClass(ext)+"\"></i>");
    }
    htm.push("</div>");
    htm.push("<div class=\"upload-hover-bg\"></div>");
    htm.push("<div class=\"upload-group-btn\">");
    if (isImgFile(file.filename)) {
        if(magnifyFile == "1"){
            htm.push("<a href=\"javascript:void(0)\"><i class=\"iconfont icon-chakan-yulan\"></i></a>");
        }
    }
    if(downloadFile == "1"){
        htm.push("<a href=\"javascript:void(0)\"><i class=\"iconfont icon-xiazai\"></i></a>");
    }
    if(removeFile == "1"){
        htm.push("<a href=\"javascript:void(0)\"><i class=\"iconfont icon-shanchu\"></i></a>");
    }
    htm.push("<input type=\"hidden\" name=\"fileName\" value=\""+file.filename+"\">");
    htm.push("<input type=\"hidden\" name=\"fileId\" value=\""+file.fileid+"\">");
    htm.push("<input type=\"hidden\" name=\"filePath\" value=\""+file.filepath+"\">");
    htm.push("</div>");
    htm.push("<p>"+file.filename+"</p>");
    htm.push("</li>");
    htm = htm.join('');
    $("#"+a_tag_id).parent().before(htm);
}
function initUploadFile(input_tag_id){
    var customerId= $("#"+input_tag_id).attr("data-customerid");
    var removeFile = $("#"+input_tag_id).attr("removeFile");//是否可以删除文件 0-否，1-是
    var downloadFile = $("#"+input_tag_id).attr("downloadFile");//是否可以下载文件 0-否，1-是
    var magnifyFile = $("#"+input_tag_id).attr("magnifyFile");//是否可以放大查看图片 0-否，1-是（只适用于图片类的文件）
    var fileId = $("#"+input_tag_id).attr("data-fileid");
    var fileType = $("#"+input_tag_id).attr("data-fileType");//上传文件后缀
    //加载已上传的附件
    loadInitFile(input_tag_id,removeFile,downloadFile,magnifyFile,fileId,customerId,fileType);
    // 删除文件
    $(document).on('click', ".upload-group-btn .icon-shanchu", function () {
        var obj = $(this).parents(".upload-group-btn");
        var fileId = obj.find('[name=fileId]').val();
        var fileName = obj.find('[name=fileName]').val();
        dggui.dialog.confire("确定删除吗？", '删除提示', function () {
            $.post(sysInfo.basePath + '/annex/del.do', {fileId: fileId, fileName: fileName}, function (result) {
                if (result.code == 0) {
                    obj.parent().remove();
                    dggui.dialog.succeed("删除成功");
                } else {
                    dggui.dialog.errors("删除失败，请联系管理员");
                }
            }, 'json');
        });
    });
    // 下载文件
    $(document).on('click', ".upload-group-btn .icon-xiazai", function () {
        var obj = $(this).parents(".upload-group-btn");
        var fileId = obj.find('[name=fileId]').val();
        var fileName = obj.find('[name=fileName]').val();
        var fileUrl = obj.find('[name=filePath]').val();
        window.location.href = sysInfo.basePath + "/annex/download.do?id=" + fileId + "&fileUrl=" + fileUrl + "&fileName=" + fileName;
    });

    // 放大图片
    $(document).on('click', ".upload-group-btn .icon-chakan-yulan", function () {
        var obj = $(this).parents(".upload-group-btn");
        var fileUrl = obj.find('[name=filePath]').val();
        var IMG = '<div><img style="width: 100%" src="' + fileUrl + '" alt=""></div>';
        layer.photos({
            photos: { "data": [{"src": fileUrl}],anim: 10}
        });
    });
}
function initUploadFile1(a_tag_id,input_tag_id){
    var customerId= $("#"+input_tag_id).attr("data-customerid");
    var removeFile = $("#"+input_tag_id).attr("removeFile");//是否可以删除文件 0-否，1-是
    var downloadFile = $("#"+input_tag_id).attr("downloadFile");//是否可以下载文件 0-否，1-是
    var magnifyFile = $("#"+input_tag_id).attr("magnifyFile");//是否可以放大查看图片 0-否，1-是（只适用于图片类的文件）
    var fileId = $("#"+input_tag_id).attr("data-fileid");

    var fileType = $("#"+input_tag_id).attr("data-fileType");//上传文件后缀
    if(isEmpty(fileType)){//如果附件后缀不存在，就修改为空
        fileType ='';
    }
    var loadFile = $("#"+input_tag_id).attr("loadFile");//初始化的时候是否加载已上传的图片 0-否，1-是
    var isOne = $("#"+input_tag_id).attr("data-isOne");//是否只能上传一个
    var multiFile = $("#"+input_tag_id).attr("data-multiple");
    if(isEmpty(isOne)){//如果没有设置，允许上传多个
        isOne=false;
    }
    //默认可选择多张
    if (multiFile == "true"||isEmpty(multiFile)) {
        multiFile=true;
    }else {
        multiFile = false;
    }
    if (loadFile == "1") {
        loadInitFile(a_tag_id,removeFile,downloadFile,magnifyFile,fileId,customerId,fileType);
    }

    //图片上传
    dggui.use('upload', function () {
        var upload = dggui.upload, uploadObj;
        $.ajax({
            type: 'POST',
            url: sysInfo.basePath + '/annex/get_file_id.do',
            data: {fileId: fileId},
            dataType: 'json',
            success: function (result) {
                if(result.code == 0){
                    fileId = result.data;
                    upload.render({
                        elem: '#'+a_tag_id
                        , url: sysInfo.basePath + '/rzsc/upload_file/upload.do'
                        , multiple: multiFile
                        , accept: 'file' //允许上传的文件类型
                        , size: 20 * 1024 //最大允许上传的文件大小
                        , data: {fileId: fileId,customerId:customerId,type:fileType,isOne:isOne}
                        , before: function (obj) {
                            uploadObj = obj;
                            dgg.dialog.loading();
                        }
                        , done: function (res) {
                            dgg.dialog.loading('close');
                            if (res.code == 0) {
                                var dataFile = res.data;
                                //预读本地文件示例，不支持ie8
                                uploadObj.preview(function (index, file, result) {
                                    if(isOne == 'true'){//移除前一个回显框
                                        $('#'+a_tag_id).parent().prev().remove();
                                    }

                                    if (file.name == dataFile.filename) {
                                        appendFile(dataFile,a_tag_id,removeFile,downloadFile,magnifyFile);
                                    }
                                });
                                dgg.dialog.succeed("上传成功");
                            } else {
                                dgg.dialog.errors(res.msg);
                            }
                        }
                    });
                }else{
                    dggui.dialog.errors("获取fileid失败！");
                }
            }
        })
    });

    // 删除文件
    $(document).on('click', ".icon-shanchu", function () {
        var obj = $(this).parents(".upload-group-btn");
        var fileId = obj.find('[name=fileId]').val();
        var fileName = obj.find('[name=fileName]').val();
        dggui.dialog.confire("确定删除吗？", '删除提示', function () {
            $.post(sysInfo.basePath + '/annex/del.do', {fileId: fileId, fileName: fileName}, function (result) {
                if (result.code == 0) {
                    obj.parent().remove();
                    dggui.dialog.succeed("删除成功");
                } else {
                    dggui.dialog.errors("删除失败，请联系管理员");
                }
            }, 'json');
        });
    });
    // 下载文件
    $(document).on('click', ".icon-xiazai", function () {
        var obj = $(this).parents(".upload-group-btn");
        var fileId = obj.find('[name=fileId]').val();
        var fileName = obj.find('[name=fileName]').val();
        var fileUrl = obj.find('[name=filePath]').val();
        window.location.href = sysInfo.basePath + "/annex/download.do?id=" + fileId + "&fileUrl=" + fileUrl + "&fileName=" + fileName;
    });

    // 放大图片
    $(document).on('click', ".icon-chakan-yulan", function () {
        var obj = $(this).parents(".upload-group-btn");
        var fileUrl = obj.find('[name=filePath]').val();
        var IMG = '<div><img style="width: 100%" src="' + fileUrl + '" alt=""></div>';
        layer.photos({
            photos: { "data": [{"src": fileUrl}],anim: 5}
        });
    });
};
function upload3(uploadId,uploadParams){
    var id = $("#"+uploadParams).attr("data-fileid");
    var flag = $("#"+uploadParams).attr("data-flag");
    var customerId = $("#"+uploadParams).attr("data-customerid");
    var isdel = $("#"+uploadParams).attr("isdel");
    var isRemove = false;
    if(isdel == 1){
        isRemove = true;
    }
    var fileup;
    dggui.use(['fileup'], function () {
      fileup = dggui.fileup;
        var opt = {
            elem: "#"+uploadId
            , isList: true // 是否开启获取初始化列表 默认为true
            , isRemove: isRemove // 是否可删除
            , isDownload: true//是否可下载
            , isPreview: true//是否可预览
            , isPrefix: true // 判断是否模糊查询
            ,size:10*1024
            , data: {fileId:customerId+"-"+flag+"-"+id,isDeleteOriginalFile: false}
            , url: sysInfo.gatewayFile //服务端上传的接口
            , urlUpload: sysInfo.uploadFile // 上传API
            , urlPrefixYes: sysInfo.getFileId // 模糊查询 API
            , urlPrefixNo: sysInfo.getFileId   // 精准查询API
            , urlDel: sysInfo.deleteFile   // 删除API
            , urlDownload: sysInfo.download   // 下载API
            , returnVal: 200
            , multiple: true
        };
        fileup.render(opt);
    })
}